package com.atguigu.mvc.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class Operation08Handler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    // 获取 ServletContext 对象的办法二：直接自动装配
    @Autowired
    private ServletContext servletContext;

    @RequestMapping("/original/servlet/api")
    public String getOriginalServletAPI(

            // 这三个是可以直接传入的
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session
    ) {

        logger.debug(request.toString());
        logger.debug(response.toString());
        logger.debug(session.toString());

        // 获取 ServletContext 对象的办法一：通过 HttpSession 对象获取
        ServletContext servletContext = session.getServletContext();
        logger.debug("办法一 servletContext -> " + servletContext.toString());
        logger.debug("办法二 servletContext -> " + this.servletContext.toString());

        return "target";
    }

}
